package www.xudan.android_ffmpeg.ijkplayer;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.MediaInfo;
import www.xudan.android_ffmpeg.R;

/**
 * @auther XuDan on 2020/3/26.
 */
public class MyBasePlayerView extends FrameLayout implements SurfaceHolder.Callback, IMediaPlayer.OnPreparedListener, IMediaInfo, IMediaPlayer.OnCompletionListener, IMediaPlayer.OnSeekCompleteListener, IMediaPlayer.OnBufferingUpdateListener, IMediaPlayer.OnVideoSizeChangedListener {
    private Context context;
    private boolean mEnableMediaCodec;
    private static IjkMediaPlayer mPlayer;
    private SurfaceView mSurfaceview;

    public MyBasePlayerView(Context context) {
        this(context, null);
    }

    public MyBasePlayerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyBasePlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        mPlayer = createPlayer();
        initView();
    }

    /**
     * 创建IjkMediaPaly
     *
     * @return
     */
    private IjkMediaPlayer createPlayer() {
        if (mPlayer == null) {
            IjkMediaPlayer ijkMediaPlayer = new IjkMediaPlayer();
            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 1);
            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", IjkMediaPlayer.SDL_FCC_RV32);
            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1);
            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);

            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "http-detect-range-support", 1);

            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "min-frames", 100);
            ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1);
            setEnableMediaCodec(ijkMediaPlayer, mEnableMediaCodec);
            setListener(ijkMediaPlayer);
            return ijkMediaPlayer;
        } else {
            return mPlayer;
        }
    }

    /**
     * 设置所有监听
     *
     * @param ijkMediaPlayer
     */
    private void setListener(IjkMediaPlayer ijkMediaPlayer) {
        ijkMediaPlayer.setOnPreparedListener(this);
        ijkMediaPlayer.setOnCompletionListener(this);
        ijkMediaPlayer.setOnSeekCompleteListener(this);
        ijkMediaPlayer.setOnBufferingUpdateListener(this);
        ijkMediaPlayer.setOnVideoSizeChangedListener(this);
    }

    //设置是否开启硬解码
    private void setEnableMediaCodec(IjkMediaPlayer ijkMediaPlayer, boolean isEnable) {
        int value = isEnable ? 1 : 0;
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", value);//开启硬解码
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", value);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", value);
    }

    public void setEnableMediaCodec(boolean isEnable) {
        mEnableMediaCodec = isEnable;
    }

    private void initView() {
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        View view = LayoutInflater.from(context).inflate(R.layout.baseplayer_layout, this, false);
        mSurfaceview = view.findViewById(R.id.surface);
        mSurfaceview.getHolder().addCallback(this);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, width * 9 / 16);
        addView(view, layoutParams);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mPlayer != null) {
            mPlayer.setDisplay(holder);
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void setPath(String path) {
        try {
            mPlayer.setDataSource(path);
            mPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPrepared(IMediaPlayer iMediaPlayer) {
        iMediaPlayer.start();
    }

    @Override
    public int getVideoWidth() {
        return mPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return mPlayer.getVideoHeight();
    }

    @Override
    public long getDuration() {
        return mPlayer.getDuration();
    }

    @Override
    public MediaInfo getMediaInfo() {
        return mPlayer.getMediaInfo();
    }

    @Override
    public void onCompletion(IMediaPlayer iMediaPlayer) {
        Log.i("xd---------", "完成");

    }

    @Override
    public void onSeekComplete(IMediaPlayer iMediaPlayer) {
        Log.i("xd---", "onSeekComplete----------" + iMediaPlayer.getCurrentPosition());
        iMediaPlayer.start();
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
    }

    @Override
    public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int i, int i1, int i2, int i3) {

    }
}
